#!/usr/bin/env python
#
#       Database.py
#       
#       Copyright 2009 Robin Hayes <bonzairob@hotmail.co.uk>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import pygame
import urllib

import Options

class Database():
    def __init__(self):
        #Doesn't need to be loaded at startup yet really but meh.
        #Maybe later for keeping track of TS games.
        self.username = ""
        self.password = ""
        self.GAME = "TurnShipDemo"
        self.loggedin = False
        if Options.AUTOLOGIN == True:
            self.userlogin(Options.USERNAME, Options.PASSWORD)
        
    def userlogin(self, username, password):
        self.username = username
        self.password = password
        
        urlparams = urllib.urlencode({'username': self.username, 'katamari': self.password, 'game': self.GAME})
        urlf = urllib.urlopen("http://pandrgames.co.uk/dbmanip/login.php", urlparams)
        print "Connecting to DB."
        print urlf.read()
        self.loggedin = True
        
    def userloginreturns(self, username, password):
        self.username = username
        self.password = password
        
        urlparams = urllib.urlencode({'username': self.username, 'katamari': self.password, 'game': self.GAME})
        urlf = urllib.urlopen("http://pandrgames.co.uk/dbmanip/login.php", urlparams)
        self.loggedin = True
        return urlf.read()
    
    def userlogout(self):
        if self.loggedin:
            urlparams = urllib.urlencode({'username': self.username, 'game': self.GAME})
            urlf = urllib.urlopen("http://pandrgames.co.uk/dbmanip/login.php", urlparams)
            print "Notifiying DB."
            print urlf.read()
            
    def userlogoutreturns(self):
        if self.loggedin:
            urlparams = urllib.urlencode({'username': self.username, 'game': self.GAME})
            urlf = urllib.urlopen("http://pandrgames.co.uk/dbmanip/login.php", urlparams)
            return urlf.read()
            
    
    def gameregister(self):
        #self.port = port
        #TODO: pull the actual port and not default.
        self.port = 7059
        if self.loggedin == True:
            urlparams = urllib.urlencode({'username': self.username, 'port': self.port,\
            'map': "TestMap", 'notes': "Anyone can join!"})
        else:
            urlparams = urllib.urlencode({'port': self.port, \
            'map': "TestMap", 'notes': "Anyone can join!"})
        #(username, port, users, map, notes)
        
        
        urlf = urllib.urlopen("http://pandrgames.co.uk/dbmanip/register.php", urlparams)
        return urlf.read()
    
    def gameunregister(self):
        urlparams = urllib.urlencode({'notes': "Delete this. Unused."})
        urlf = urllib.urlopen("http://pandrgames.co.uk/dbmanip/register.php", urlparams)
        return urlf.read()